package gov.nhso.prov.acc.apps.acc7;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.isud.core.ISUD;
import com.isud.core.ISUDFactory;
import com.isud.paging.PageBean;

import gov.nhso.prov.acc.beans.Acc7IncomeBean;
import gov.nhso.prov.acc.beans.Acc7TransactionBean;
import gov.nhso.prov.acc.core.DBConnection;
import gov.nhso.prov.acc.utils.Utility;

public class ACC70303 extends Utility{
	public String url = "/apps/acc7/ACC70303A.jsp";

	public ACC70303(HttpServletRequest request, HttpServletResponse response) {
		this.message(request).clearMsg();
		String job = this.getAttr(request, "job");
		
		System.out.println("File name > ACC70303" );
		System.out.println("Job > " + job);
		System.out.println("Line > 28");
		
		if(job.equals("init")){
			this.clearLastRequest(request, "lastSearch");
			this.init(request);
		} else if (job.equals("add")){
			this.add(request);
		} else if (job.equals("search")){
			this.setLastRequest(request, "lastSearch");
			this.search(request);
		} else if (job.equals("save")){
			this.save(request);
		} else if (job.equals("viewEdit")){
			this.viewEdit(request);
		} else if (job.equals("back")){
			if (this.getLastRequest(request, "lastSearch")) {
				this.search(request);
			} else {
				this.init(request);
			}
		} else if (job.equals("edit")){
			this.edit(request);
		} else if (job.equals("confirm")){
			this.confirm(request);
		} else if (job.equals("del")){
			this.del(request);
		}
	}
	private String init(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			
			request.setAttribute("listBudgetYear", this.dataUtil(dao).lsBudgetYear());
			request.setAttribute("job", "search");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}

	private String search(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);

			PageBean pageBean = new PageBean();
			Acc7IncomeBean incomeBean = new Acc7IncomeBean();
			this.beanUtil().copyRequest2Bean(request, incomeBean);
			String docNo = this.getAttr(request, "docNo");
			String provinceId = this.getAttr(request, "provinceId");
			String startDate = this.getAttr(request, "startDate");
			String endDate = this.getAttr(request, "endDate");
			String refNo = this.getAttr(request, "refNo");
			String condition = "";
			
			if(!dao.chkData(startDate).equals("")){
				startDate = this.dateUtil().changeFormatDate(startDate, "/", "/");
				condition += " AND TRUNC(INCOME_DATE) >= TO_DATE('"+startDate+"','DD/MM/YYYY') ";
			}			
			if(!dao.chkData(endDate).equals("")){
				endDate = this.dateUtil().changeFormatDate(endDate, "/", "/");
				condition += " AND TRUNC(INCOME_DATE) <= TO_DATE('"+endDate+"','DD/MM/YYYY') ";
			}
			incomeBean.setRefNo(refNo);
			incomeBean.setProvinceId(provinceId);
			incomeBean.setDocNo(docNo);
			incomeBean.setActive("Y");
			if (this.user(request).getAreaLevel().equals("2")){	
				condition = " AND PROVINCE_ID IN (SELECT PROVINCE_ID FROM PROVINCE WHERE NHSO_ZONE = '" + this.user(request).getAreaCode()+ "' ) ";
			}
			condition += " AND DOC_NO LIKE '%B%'";
			this.beanUtil().copyRequest2Bean(request, pageBean);
			pageBean = dao.selectPage(incomeBean,condition, pageBean);
			
			if (pageBean.getPageData() == null) {
				this.message(request).setMsgCode("I001");
			}
			request.setAttribute("pageBean", pageBean);
			request.setAttribute("listBudgetYear", this.dataUtil(dao).lsBudgetYear());
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String add(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);

			Acc7IncomeBean incomeBean = new Acc7IncomeBean();

			incomeBean.setDocNo("สร้างจากระบบ");
			request.setAttribute("incomeBean", incomeBean);
			request.setAttribute("job", "save");
			request.setAttribute("listBudgetYear", this.dataUtil(dao).lsBudgetYear());
			request.setAttribute("now", new Date());
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc7/ACC70303B.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String save(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);

			Acc7IncomeBean incomeBean = new Acc7IncomeBean();
			this.beanUtil().copyRequest2Bean(request, incomeBean);
			
			incomeBean.setDocNo(this.genCode(dao).getIncomeDocNoAcc7(incomeBean.getBudgetYear(), incomeBean.getProvinceId(), "B"));
			incomeBean.setIncomeDate(this.dateUtil().changeDateToDB(incomeBean.getIncomeDate()));
			//incomeBean.setRefNo(this.encodeUTF(incomeBean.getRefNo()));
			incomeBean.setIncomeAmount(incomeBean.getIncomeAmount().replace(",", ""));
			incomeBean.setUpdatedBy(this.user(request).getUserId());
			incomeBean.setCreatedBy(this.user(request).getUserId());
			dao.insert(incomeBean);
			
			request.setAttribute("docNo", incomeBean.getDocNo());
			this.message(request).setMsgCode("I002");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.viewEdit(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String edit(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);

			Acc7IncomeBean incomeBean = new Acc7IncomeBean();
			this.beanUtil().copyRequest2Bean(request, incomeBean);
			
			incomeBean.setIncomeDate(this.dateUtil().changeDateToDB(incomeBean.getIncomeDate()));
			incomeBean.setIncomeAmount(incomeBean.getIncomeAmount().replace(",", ""));
			if (incomeBean.getIncomeDesc().equals("")){
				incomeBean.setIncomeDesc("null");
			}
			//incomeBean.setRefNo(this.encodeUTF(incomeBean.getRefNo()));
			incomeBean.setUpdatedBy(this.user(request).getUserId());
			incomeBean.setUpdatedDate(this.sysDate);
			dao.update(incomeBean);
			
			request.setAttribute("docNo", incomeBean.getDocNo());
			this.message(request).setMsgCode("I002");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.viewEdit(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String viewEdit(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			
			String docNo = this.getAttr(request, "docNo");
			String budgetYear = this.getAttr(request, "budgetYear");
			String provinceId = this.getAttr(request, "provinceId");
			Acc7IncomeBean incomeBean = new Acc7IncomeBean();
			incomeBean.setDocNo(docNo);
			incomeBean.setBudgetYear(budgetYear);
			incomeBean.setProvinceId(provinceId);
			incomeBean = (Acc7IncomeBean)dao.select(incomeBean);
			
			request.setAttribute("incomeBean", incomeBean);
			request.setAttribute("job", "edit");
			request.setAttribute("listBudgetYear", this.dataUtil(dao).lsBudgetYear());
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc7/ACC70303B.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String confirm(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			
			String docNo = this.getAttr(request, "docNo");
			String budgetYear = this.getAttr(request, "budgetYear");
			String provinceId = this.getAttr(request, "provinceId");
			Acc7IncomeBean incomeBean = new Acc7IncomeBean();
			Acc7IncomeBean bean = new Acc7IncomeBean();
			Acc7TransactionBean transactionBean = new Acc7TransactionBean();
			String ts = "";
			ts = this.dateUtil().getCurrentTimeStamp2DB();

			if(!docNo.equals("สร้างจากระบบ")){
				this.beanUtil().copyRequest2Bean(request, incomeBean);
				bean.setDocNo(docNo);
				bean.setProvinceId(provinceId);
				bean.setBudgetYear(budgetYear);
				dao.select(bean);
				if (bean.getIncomeStatus().equals("P")) {
					incomeBean.setIncomeDate(this.dateUtil().changeDateToDB(incomeBean.getIncomeDate()));
					incomeBean.setIncomeAmount(incomeBean.getIncomeAmount().replace(",", ""));
					incomeBean.setDocNo(docNo);
					//incomeBean.setRefNo(this.encodeUTF(incomeBean.getRefNo()));
					incomeBean.setBudgetYear(budgetYear);
					incomeBean.setProvinceId(provinceId);
					incomeBean.setIncomeStatus("Y");
					incomeBean.setTransactionTimestamp(this.dateUtil().setTimeStamp2DB(ts));
					incomeBean.setUpdatedBy(this.user(request).getUserId());
					incomeBean.setUpdatedDate(this.sysDate);
					dao.update(incomeBean);

					this.beanUtil().copyRequest2Bean(request, incomeBean);
					transactionBean.setBudgetYear(budgetYear);
					transactionBean.setProvinceId(incomeBean.getProvinceId());
					transactionBean.setIncomeAmount(incomeBean.getIncomeAmount().replace(",", ""));
					transactionBean.setIncomeDate(this.dateUtil().changeDateToDB(incomeBean.getIncomeDate()));
					transactionBean.setReasonCode("00");
					transactionBean.setTransactionType("B");
					transactionBean.setTransactionTimestamp(this.dateUtil().setTimeStamp2DB(ts));
					//transactionBean.setRefNo(incomeBean.getRefNo());
					dao.insert(transactionBean);
					this.message(request).setMsgCode("I002");
				} else {
					this.message(request).setMsg("E", "ไม่สามารถยืนยันได้ สถานะไม่ได้อยู่เตรียมดำเนินการ กรุณาตรวจสอบ");
				}
			}else{
				this.beanUtil().copyRequest2Bean(request, incomeBean);
				incomeBean.setDocNo(this.genCode(dao).getIncomeDocNoAcc7(incomeBean.getBudgetYear(), incomeBean.getProvinceId(), "B"));
				incomeBean.setTransactionTimestamp(this.dateUtil().setTimeStamp2DB(ts));
				incomeBean.setIncomeDate(this.dateUtil().changeDateToDB(incomeBean.getIncomeDate()));
				incomeBean.setIncomeAmount(incomeBean.getIncomeAmount().replace(",", ""));
				incomeBean.setIncomeStatus("Y");
				//incomeBean.setRefNo(this.encodeUTF(incomeBean.getRefNo()));
				incomeBean.setUpdatedBy(this.user(request).getUserId());
				incomeBean.setCreatedBy(this.user(request).getUserId());
				dao.insert(incomeBean);
				
				transactionBean.setBudgetYear(budgetYear);
				transactionBean.setProvinceId(incomeBean.getProvinceId());
				transactionBean.setIncomeAmount(incomeBean.getIncomeAmount().replace(",", ""));
				transactionBean.setIncomeDate(incomeBean.getIncomeDate());
				transactionBean.setReasonCode("00");
				transactionBean.setTransactionType("B");
				transactionBean.setTransactionTimestamp(this.dateUtil().setTimeStamp2DB(ts));
				//transactionBean.setRefNo(incomeBean.getRefNo());
				dao.insert(transactionBean);
				
				docNo = incomeBean.getDocNo();
				budgetYear = incomeBean.getBudgetYear();
				provinceId = incomeBean.getProvinceId();
				this.message(request).setMsgCode("I002");
			}
			
			request.setAttribute("docNo", docNo);
			request.setAttribute("budgetYear", budgetYear);
			request.setAttribute("provinceId", provinceId);
			request.setAttribute("listBudgetYear", this.dataUtil(dao).lsBudgetYear());
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.viewEdit(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}

	private String del(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			
			String docNo = this.getAttr(request, "docNo");
			String budgetYear = this.getAttr(request, "budgetYear");
			String provinceId = this.getAttr(request, "provinceId");
			Acc7IncomeBean incomeBean = new Acc7IncomeBean();
			
			incomeBean.setDocNo(docNo);
			incomeBean.setBudgetYear(budgetYear);
			incomeBean.setProvinceId(provinceId);
			incomeBean.setActive("N");
			dao.update(incomeBean);
			
			request.setAttribute("docNo", "");
			this.message(request).setMsgCode("I002");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.search(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
}
